package zzti.com.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.PowerManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import zzti.com.hongbao.R;

/**
 * Created by ASUS-PC on 2016/4/19.
 */


@SuppressLint("NewApi")
public class NotfLinstenerService extends NotificationListenerService{
    private String name;
    private PowerManager.WakeLock wakeLock=null;
    private NotificationManager manager;
    private boolean judge=true;

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        Log.v("aa","bbbb");
        Log.v("aa",sbn.getNotification().extras.getString(Notification.EXTRA_TEXT));
        name=sbn.getNotification().extras.getString(Notification.EXTRA_TEXT);
        Log.v("aa","name is "+name);
        if(name!=null&&(name.contains("红包")||name.contains("恭喜发财"))){
            if(wakeLock==null&&true){
                PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
                wakeLock=pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP,this.getClass().getCanonicalName());
                wakeLock.acquire();
                manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification2=new Notification.Builder(this).setTicker("红包来了").setContentText("红包来了快抢").setContentTitle("红包").build();
                notification2.sound= Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hb);
              // notification2.defaults=Notification.DEFAULT_SOUND;
                manager.notify(1,notification2);
                Log.v("aa","notification");
                judge=false;
            }
        }
        if(wakeLock!=null&&wakeLock.isHeld()){
            wakeLock.release();
            wakeLock=null;

           // manager.cancel(1);
        }

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

}
